package kuzmenkov.applet;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;

/**
 * The class listens action events of the view panel that shows image components
 * with screens
 * 
 * @author Michael Kuzmenkov
 * 
 */
public class ViewActionListener implements ActionListener {
	private Logger log = Logger.getLogger(this.getClass().getPackage()
			.getName());

	private ViewerApplet viewerApplet;

	public ViewActionListener(ViewerApplet viewerApplet) {
		if (viewerApplet == null)
			throw new IllegalArgumentException("The argument viewerApplet is null");
		this.viewerApplet = viewerApplet;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		log.fine(e.getActionCommand());

		String command = e.getActionCommand();
		assert (command!=null&&!command.isEmpty());
		Component currComp = viewerApplet.getCurrImageComp();

		if (currComp == null){
			assert (false);
			return;
		}

		Container parent = currComp.getParent();
		if (parent==null){
			assert(false);
			return;
		}
			
		if (ViewerApplet.REMOVE_MENU_ITEM.equals(command)) {
			remove(currComp, parent);
		} else if (ViewerApplet.MOVE_FORWARD_MENU_ITEM.equals(command)) {
			moveForward(currComp, parent);
		} else if (ViewerApplet.MOVE_BACK_MENU_ITEM.equals(command)) {
			moveBack(currComp, parent);
		} else if (ViewerApplet.MOVE_AT_THE_BEGINNING_MENU_ITEM.equals(command)) {
			moveAtTheBeginning(currComp, parent);
		} else if (ViewerApplet.MOVE_AT_THE_END_MENU_ITEM.equals(command)) {
			moveAtTheEnd(currComp, parent);
		}

		parent.doLayout();
		parent.repaint();
	}

	/**
	 * Moves back the component through the list on the container
	 * @param comp the component
	 * @param parent the container
	 */
	private void moveBack(Component comp, Container parent) {
		int index = getIndex(comp, parent.getComponents());
		if (index < 0) {
			log.warning("The component haven't been found");
			return;
		}
		parent.remove(index);
		parent.add(comp, index - 1);
	}

	/**
	 * Moves forward the component through the list on the container
	 * @param comp the component
	 * @param parent the container
	 */
	private void moveForward(Component comp, Container parent) {
		int index = getIndex(comp, parent.getComponents());
		if (index < 0) {
			log.warning("The component haven't been found");
			return;
		}
		parent.remove(index);
		parent.add(comp, index + 1);
	}

	private int getIndex(Object object, Object[] objects) {
		for (int i = 0; i < objects.length; i++) {
			if (objects[i] == object) {
				return i;
			}
		}

		return -1;
	}

	/**
	 * Moves the component on the container at the end of the list  
	 * @param comp the component
	 * @param parent the container
	 */
	private void moveAtTheEnd(Component comp, Container parent) {
		parent.remove(comp);
		parent.add(comp, -1);

	}

	/**
	 * Moves the component on the container at the beginning of the list
	 * @param comp the component
	 * @param parent the container
	 */
	private void moveAtTheBeginning(Component comp, Container parent) {
		parent.remove(comp);
		parent.add(comp, 0);
	}

	/**
	 * Removes the component from the container
	 * @param comp the component
	 * @param parent the container
	 */
	private void remove(Component comp, Container parent) {
		parent.remove(comp);
		viewerApplet.updatePanelSizes();
	}

}
